/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Chitietphieunhaphang;
import pojo.Mathang;
import pojo.Phieunhaphang;
import util.HibernateUtil;

/**
 *
 * @author FT
 */
public class ChiTietPhieuNhapHangDAO {

    public static List<Chitietphieunhaphang> layDanhSachChiTietPhieuNhapHang() {
        List<Chitietphieunhaphang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select sv from Chitietphieunhaphang sv";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static boolean Insert(String mact, String maphieu, String mathang, int soluong, int status) {
          Session session = HibernateUtil.getSessionFactory().openSession();
           Chitietphieunhaphang ct = new Chitietphieunhaphang(mact);
            ct.setIdchitietphieunhaphang(mact);
//            session.save(ct);
            
           
            ct.setPhieunhaphang(new Phieunhaphang(maphieu));
            
            ct.setMathang(new Mathang(mathang));
            ct.setSoluong(soluong);
            if (status == 1){
                MatHangDAO.ThemMatHang(new Mathang(mathang), soluong);
            }
            
        try {
            session.beginTransaction();
           
            session.save(ct);
            session.getTransaction().commit();
            
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
            
        }
        return true;
    }
    
    

    public static String MaTangTuDong() {
        String chuoi = "";
        String macuoicung = "";
        List<Chitietphieunhaphang> listpnh = ChiTietPhieuNhapHangDAO.layDanhSachChiTietPhieuNhapHang();
        if (ChiTietPhieuNhapHangDAO.layDanhSachChiTietPhieuNhapHang().size()==0){
            chuoi += "ctn001";
            return chuoi;
        }
        macuoicung += listpnh.get(ChiTietPhieuNhapHangDAO.layDanhSachChiTietPhieuNhapHang().size() - 1).getIdchitietphieunhaphang();
        String index = macuoicung.substring(3);
        try {

            //System.out.println("Index la"+index+"");
            int chiso = Integer.valueOf(index);
            chiso++;

            if (chiso < 10) {
                chuoi += "ctn00" + chiso;
            } else if (chiso >= 10 && chiso < 100) {
                chuoi += "ctn0" + chiso;
            } else if (chiso >= 1000) {
                chuoi += "ctn" + chiso;
            }
        } catch (Exception e) {
            chuoi += "NA";
        }
        return chuoi;
    }
    
    
    public static Chitietphieunhaphang getChiTietPhieuNhapHang(String id){
        Chitietphieunhaphang ctp = new Chitietphieunhaphang();
        List<Chitietphieunhaphang> listctp = ChiTietPhieuNhapHangDAO.layDanhSachChiTietPhieuNhapHang();
        for (int i=0;i<listctp.size();i++){
            if (id.equals(listctp.get(i).getIdchitietphieunhaphang()))
                ctp = listctp.get(i);
        }
        return ctp;
    }
    
    public static void DeleteOne(String machitiet){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Chitietphieunhaphang ctpnh = ChiTietPhieuNhapHangDAO.getChiTietPhieuNhapHang(machitiet);
        try {
            session.beginTransaction();
            session.delete(ctpnh);
            session.getTransaction().commit();
            
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
            
        }
    }
    
    public static void DeleteAll(String maphieunhap){
        List<Chitietphieunhaphang> listctp = ChiTietPhieuNhapHangDAO.layDanhSachChiTietPhieuNhapHang();
        for (int i=0;i<listctp.size();i++){
            if(listctp.get(i).getPhieunhaphang().getIdphieunhaphang().equals(maphieunhap)){
                ChiTietPhieuNhapHangDAO.DeleteOne(listctp.get(i).getIdchitietphieunhaphang());
            }
        }
    }
}
